﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Lab6
{
    public partial class Child : Form
    {
        private const string CLP_FORMAT = "GRAPHIC_EDITOR_DATA";
        string fileName;
        Shape rubber = null;
        public DrawMode drawMode = DrawMode.dmRect;
        Shape selectedShape = null;
        private List<Shape> shapeList = new List<Shape>();
        private Point moveOrigin;

        public Image childImage
        {
            get;
            set;
        }
        public Child(int width, int height)
            : base()
        {
            InitializeComponent();


            childImage = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(childImage);
            g.Clear(Color.Blue);

            g.Dispose();
            this.AutoScrollMinSize = new Size(width, height);

        }

        public Child(Image image, string FileName)
        {

            InitializeComponent();
            childImage = new Bitmap(image);
            fileName = FileName;

            this.AutoScrollMinSize = childImage.Size;


        }

        private void Child_Load(object sender, EventArgs e)
        {

        }

        private void paintWindow(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //code from http://www.bobpowell.net/understanding_autoscroll.htm
            //Matrix mx = new Matrix(1, 0, 0, 1, AutoScrollPosition.X, AutoScrollPosition.Y);
            //e.Graphics.Transform = mx;
            //g.DrawImage(childImage, 0, 0, childImage.Width, childImage.Height);
            foreach (Shape s in shapeList)
            {
                s.Draw(g);
            }
            if (selectedShape != null)
            {
                Rectangle r = selectedShape.GetBoundingRect();
                r.Inflate(2, 2);
                g.DrawRectangle(new Pen(Color.Red), r);
            }
            g.Dispose();

        }

        private void Child_MouseDown(object sender, MouseEventArgs e)
        {
            Point p = new Point (e.X , e.Y );
            switch (drawMode)
            {
                case DrawMode.dmRect:
                    if (rubber == null)
                    {
                        rubber = new Rect();
                        rubber.AddPoint(new Point(e.X, e.Y));
                    }
                    break;
                case DrawMode.dmPolygon:
                    if (rubber == null) 
                    {
                        rubber = new Polygon();
                        rubber.AddPoint(new Point(e.X, e.Y));
                    }
                    else if (rubber.IsEnd(new Point(e.X, e.Y)))
                    {
                        rubber.RemoveAt(rubber.NumOfPoints - 1);
                        shapeList.Add(rubber);
                        rubber = null;
                        Invalidate();

                    }
                    else
                    {
                        rubber.AddPoint(new Point(e.X, e.Y));
                    
                    }

                    break;

                case DrawMode.dmSelect:
                    selectedShape = null;
                    for (int i = shapeList.Count - 1; i >= 0; i--)
                    {
                        Shape s = shapeList[i];
                        if (s.PtInShape(new Point(e.X, e.Y)))
                        {
                            selectedShape = s;
                            break;
                        }
                    }

                    Invalidate();
                    if (selectedShape != null)
                    {
                        moveOrigin.X = e.X;
                        moveOrigin.Y = e.Y;
                    }
                    break;
            }
        }

        private void Child_MouseMove(object sender, MouseEventArgs e)
        {
            switch (drawMode)
            {
                case DrawMode.dmRect:
                    if (rubber != null && e.Button == MouseButtons.Left)
                    {

                        Invalidate();
                        Update();
                        if (rubber.NumOfPoints == 1)
                        {
                            rubber.AddPoint(new Point(e.X, e.Y));
                        }
                        else
                        {
                            rubber[1] = new Point(e.X, e.Y);
                        }
                        Graphics g = CreateGraphics();
                        rubber.Draw(g);
                        g.Dispose();

                    }
                    break;

                case DrawMode.dmPolygon:
                    if (rubber != null)
                    {
                        Invalidate();
                        Update();
                        if (rubber.NumOfPoints == 1)
                        {
                            rubber.AddPoint(new Point(e.X, e.Y));
                        }
                        else
                        {
                            rubber[rubber.NumOfPoints - 1] = new Point(e.X, e.Y);
                        }
                        Graphics g = CreateGraphics();
                        rubber.Draw(g);
                        g.Dispose();
                    }
                        break;
                case DrawMode.dmSelect:
                    if (e.Button == MouseButtons.Left && selectedShape != null)
                    {
                        selectedShape.Offset(e.X - moveOrigin.X, e.Y - moveOrigin.Y);
                        moveOrigin.X = e.X;
                        moveOrigin.Y = e.Y;
                    }
                    Invalidate();
                    Update();
                    break;
            }

        }

        

        public void CopyShape()
        {
            if (selectedShape != null)
            {
                DataFormats.Format format = DataFormats.GetFormat(CLP_FORMAT);
                IDataObject ido = new DataObject();
                ido.SetData(format.Name, false, selectedShape);
                Clipboard.SetDataObject(ido, true);
            }

        }

        public void PasteShape()
        {
            IDataObject ido2 = Clipboard.GetDataObject();
            if (ido2.GetDataPresent(CLP_FORMAT))
            {
                Shape s = ido2.GetData(CLP_FORMAT) as Shape;
                s.Offset(10, 10);
                shapeList.Add(s);
                Invalidate();
            }
        }

        private void Child_MouseUp(object sender, MouseEventArgs e)
        {

            if (drawMode != DrawMode.dmPolygon && rubber != null)
            {
                shapeList.Add(rubber);
                rubber = null;
                Invalidate();
            }
        }


    }
}
